﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Muzo.Core.Helpers
{
    public class SerializationHelper
    {
        public static string XmlSerialize(object obj)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
            using (MemoryStream memoryStream = new MemoryStream())
            {
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                xmlSerializer.Serialize(xmlTextWriter, obj);
                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
        }

        public static T XmlDeSerialize<T>(string strXml)
        {
            Type type = typeof(T);
            XmlSerializer xmlSerializer = new XmlSerializer(type);
            using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(strXml)))
            {
                object obj = xmlSerializer.Deserialize(memoryStream);

                return (T)obj;
            }
        }
    }
}